﻿using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;

namespace BetterIO.Tests
{
    [TestClass]
    public class InMemoryFileSystemStoreTests
    {
        [TestMethod]
        public void Test_Constructor()
        {
            var store = new InMemoryFileSystemStore();
            store.CheckInternalDirectoriesConsistency();
            CollectionAssert.AreEquivalent(store.GetLogicalDrives(), new[] { @"C:\"});

            store = new InMemoryFileSystemStore(@"C:\", @"D:\");
            Assert.AreEqual(store.GetLogicalDrives().Length, 2);
            store.CheckInternalDirectoriesConsistency();
            CollectionAssert.AreEquivalent(store.GetLogicalDrives(), new[] { @"C:\", @"D:\" });
        }
        
        [TestMethod]
        public void Test_CreateDirectory()
        {
            var store = new InMemoryFileSystemStore();
            var directoryInfo = store.CreateDirectory(@"C:\directory1");
            Assert.AreEqual(directoryInfo.FullName, @"C:\directory1");
            Assert.AreEqual(FileAttributes.Directory, directoryInfo.Attributes);

            directoryInfo = store.CreateDirectory(@"C:\directory1\..\d2");
            Assert.AreEqual(directoryInfo.FullName, @"C:\d2");
            Assert.AreEqual(FileAttributes.Directory, directoryInfo.Attributes);

            directoryInfo = store.CreateDirectory(@"C:\1\2\3\4\5");
            Assert.AreEqual(directoryInfo.FullName, @"C:\1\2\3\4\5");
            Assert.AreEqual(FileAttributes.Directory, directoryInfo.Attributes);
            Assert.IsTrue(store.GetDirectoryInfo(@"C:\1").Exists);
            Assert.IsTrue(store.GetDirectoryInfo(@"C:\1\2").Exists);
            Assert.IsTrue(store.GetDirectoryInfo(@"C:\1\2\3").Exists);

        }

        [TestMethod]
        public void Test_CreateDirectory_Exceptions()
        {
            var store = new InMemoryFileSystemStore();
            Helper.ExpectException<ArgumentNullException>(() => store.CreateDirectory(null));
            Helper.ExpectException<ArgumentException>(() => store.CreateDirectory(string.Empty));
            Helper.ExpectException<ArgumentException>(() => store.CreateDirectory("   "));
            Helper.ExpectException<ArgumentException>(() => store.CreateDirectory(""));
            
            LongPathTools.Instance.GetInvalidPathChars().Select(t => @"C:\folder" + t).ForEach(t => Helper.ExpectException<ArgumentException>(() => store.CreateDirectory(t)));

            // TODO: check IOException when file exists
        }

    }
}
